home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / A / AxoCalculator Package / AxoCalculator Documentation / Programming in C / The C Language < prev   
Encoding:
Text File  |  1993-03-13  |  10.6 KB  |  316 lines  |  [TEXT/AxoC]

  1.  
  2. AxoCalculator's C Programming Language
  3.  
  4. Contents
  5.  
  6. •    Introduction
  7. •    Strings
  8. •    Comments and Continuation Lines
  9. •    Dialogs
  10. •    Conditional Branch
  11. •    Loop Commands
  12. •    Variable Declaration
  13. •    Procedures, Functions and Programs
  14. •    Declaring a Procedure
  15. •    Declaring a Function
  16. •    Adding a Program to the Calculator Menu
  17. •    Auto-loading Programs
  18.  
  19. Introduction
  20.  
  21. AxoCalculator implements several different programming languages. This document describes its implementation of the C language. True C is a large, complex language. AxoCalculator implements a simplified subset of C. If you are familiar with C, check the following carefully to find out what you can and can't do in AxoCalculator's C. In addition to omitting many features, some of the strict requirements of C have been relaxed to make programming AxoCalculator easier. For example, C is case sensitive, but AxoCalculator C is not. C requires that all variables be declared and that all statements end with a semicolon. These requirements make it laborious to perform simple calculations. For example, to set two variables, a and b, to 10 and 20 respectively, add them together, then output the result you would need to type the following using true C :
  22.  
  23. float a, b;
  24. a = 10;
  25. b = 20;
  26. printf (a+b);
  27.  
  28.  
  29.  
  30. In contrast, AxoCalculator's C language does not require variables to be declared, ignores semicolons and outputs the result of any expression which lacks an assignment operator. This means you only have to type :
  31.  
  32. a = 10
  33. b = 20
  34. a + b
  35.  
  36.  
  37. AxoCalculator can execute both of the above programs. First choose "Settings…" under the "Calculator" menu, and make sure the programming language is set to C. To execute one or more lines of code, select (highlight) them using the mouse, then press the "enter" key.
  38.  
  39.  
  40. Strings
  41.  
  42. Character strings are enclosed in double quotes (e.g. "This is a string"). To include a double quote, tab or return in a character string, use 
  43.     \"  for a quote,  
  44.     \t  for a tab 
  45.     \n or \r  for a return. 
  46.  
  47. String variables can be created in two ways. The simplest is to assign a string to a previously unused variable name. For example,
  48.  
  49. aString = "Hello world"
  50.  
  51. As an alternative, use the "NewString" procedure.
  52.  
  53. NewString (bString)
  54. bString="abcde"
  55.  
  56. Strings can be appended to one another using the "Concat" function. 
  57.  
  58. NewString (cString)
  59. cString = concat (aString, "  ", bString)
  60.  
  61.  
  62. Individual characters in a string can be accessed and manipulated. A string variable behaves like a 255 element array. Each element is one character. Accessing an element returns the ASCII code of that character. The length of the string is stored in the zero indexed element. 
  63.  
  64. strLen = bString(0)
  65. char3 = bString(3)
  66. Print ("\nLength = ",strLen, "   ASCII code of 3rd char = ",char3)
  67.  
  68. Comments and Continuation Lines
  69.  
  70. Comments are enclosed in slash, star pairs:  /* a comment */. 
  71. An ampersand ( & ) as the first character of a line indicates a continuation line.
  72.  
  73. For example, select the following 6 lines then press "enter".
  74.  
  75. /* Demonstrate comments, continuation lines and character strings */
  76. printf ("\nCell B's dose-response data\nDose\tResponse\n",
  77. &             10,"    \t",2.3,"\n",
  78. &            30,"    \t",5.5,"\n",
  79. &             100,"   \t",10.4,"\n",
  80. &             300,"   \t",11.0)
  81.  
  82.  
  83. Dialogs
  84.  
  85. AxoCalculator programs can interact with the user via standard dialogs. This is done using two built in procedure calls, "Alert" and "PoseDialog". These procedures are described in the "Built in Procedures" document. "Alert" is used for simple messages or for returning results. "PoseDialog" is used for requesting one or more numerical values. An example program follows :
  86.  
  87. PoseDialog ("\n Calculate the volume and surface area of a sphere", 
  88. &                        "Radius of sphere ",radius)
  89. theSA = 4 * pi * radius ^ 2
  90. theVolume = (4 / 3) * pi * radius ^ 3
  91. Alert ("The volume and surface area of a \n sphere with radius ", radius,
  92. &           " are : \n Volume = ",theVolume, "\n Surface area = ",theSA)
  93.  
  94.  
  95. Conditional Branch
  96.  
  97. AxoCalculator supports the standard "if   else" statement for conditional branching. A program demonstrating this statement follows :
  98.  
  99. /* This program finds the square root of a number entered by the user */
  100. PoseDialog ("\n Find the square root of a number", "Enter the number", a)
  101. if (a < 0) 
  102.     Alert ("Can't calculate the square root of a negative number : ",a)
  103. else {
  104.     b = sqrt (a)
  105.     Alert ("\n The square root of ",a,"is ",b)
  106. }
  107.  
  108.  
  109. Note : because the "else" condition required two lines to be executed,
  110.         these lines are enclosed in curly brackets.
  111.  
  112.  
  113.  
  114. AxoCalculator's C language relaxes the use of semi-colons after every statement. This makes writing simple programs easier, but can lead to ambiguities with "if   else" statements. Do not use the following styles,
  115.  
  116. A)  "if   else  " all on the same line
  117. if (condition1) then action1 else action2
  118.  
  119. Instead use,
  120. if (condition1) action1 
  121. else action2
  122.  
  123. or,
  124. if (condition1) 
  125.     action1 
  126. else 
  127.     action2
  128.  
  129. B)  "else if' " extending over more than one line
  130. if (condition1)  
  131.     action1
  132. else if (condition2)  
  133.     action2
  134. else
  135.     action3
  136.  
  137. Instead use,
  138. if (condition1)  
  139.     action1
  140. else {
  141.     if (condition2)  
  142.         action2
  143.     else
  144.         action3
  145. }
  146.  
  147.  
  148.  
  149.  
  150.  
  151. Loop Commands
  152. AxoCalculator supports several commands for executing a group of statements multiple times. These loop commands are "For", "While" and "Do  While". Executable programs demonstrating each of these commands follow. 
  153.  
  154. Note : To interrupt a running program (for example to escape from
  155.           an infinite loop) press the "esc" key, or the Cmd-period 
  156.           key combination.
  157.  
  158. •    The "For" command
  159.  
  160. For ( i = 1 ; i <= 5 ; i++ ) {
  161.     a = i * 5
  162.     b = sin (a)
  163.     printf ("sin ( ",a,") = ",b)
  164. }
  165.  
  166. • The "While" command
  167.  
  168. a = 6
  169. While (a > 0) {
  170.     a--
  171.     b = exp (a)
  172.     printf ("exp ( ",a,") = ",b)
  173. }
  174.  
  175. • The "Do  While" command
  176.  
  177. a = 0
  178. b = 0
  179. Do {
  180.     a += 1
  181.     b += 2
  182.     c = b ^ a
  183.     printf (b,"^ ",a," = ",c)
  184. }
  185. While (a <= 5)
  186.  
  187. These loop commands can also be used to execute a single line expression multiple times. In this situation, curly brackets are not needed. For example :
  188.  
  189. For ( i = 1 ; i <= 20 ; i++ ) write (i)
  190.  
  191.  
  192. Variable Declaration
  193.  
  194. Variables created without being declared (as in the above examples) are always "float" (i.e. floating point). To work with Integer, Boolean or Array variables, they must be declared in standard C fashion. For example :
  195.  
  196.     int i,j
  197.     boolean b
  198.     float r
  199.     float anArr[50]
  200.     string aStr
  201.  
  202. Each variable name is preceded by the type of the variable. For array variables, the size of the array is also specified. Only float Arrays are supported. 
  203.  
  204. Arrays can be created anywhere in a program using the "NewArray" procedure,
  205.  
  206.     NewArray (anArray, arraySize)
  207.  
  208. Variables may be declared as either global (available to all programs and procedures) or local (available only to the currently active procedure). Global variables are preserved until they are explicitly unloaded. Local variables, which are declared within a function, are automatically unloaded when the function finishes executing. Local variables are declared immediately after a "Function" or "Program" declaration line (see below), after the first "{", and before the first statement. Global variables are declared before any "Function" or "Program" declarations. Examples of both local and global variable declaration can be found in the "Example Programs" document.
  209.  
  210.  
  211.  
  212. Procedures, Functions and Programs
  213.  
  214. AxoCalculator's programming language can be extended by loading user defined Procedures, Functions and Programs. These all have the same basic form : 
  215.  
  216. •  a declaration line which includes a name and lists any parameters 
  217. •  an open curly bracket. 
  218. •  an optional local variable declaration section
  219. •  a program section ending with a close curly bracket. 
  220. •  an optional "return" statement. 
  221.  
  222. Parameters are always passed by value.
  223. Passing parameter by reference is not supported.
  224. Programs can not have any parameters. 
  225.  
  226.  
  227. A Procedures, Function or Program can be loaded by selecting it's text, then pressing "enter" or choosing "Load" under the "Calculator" menu. The only difference is that "Load" will not attempt to execute any lines of code outside the program declaration, and may therefore produce a clearer error message.
  228.  
  229. To run a Procedures, Function or Program, type in its name followed by any parameters. A Function may be executed as part of a numerical expression. Programs and Functions may call other Programs and Functions. 
  230.  
  231. Declaring a Procedure
  232.  
  233. Procedures in C are simply functions that return "Void". Here is an example of how to declare a simple Procedure with a single parameter. This procedure has no local variable declaration section. To load it, select the following 4 lines, and press "enter". To run it, type "CountTo(10)" then "enter".
  234.  
  235. void CountTo (Number)
  236. {
  237.     For ( j = 1 ; j <= Number ; j++ ) printf (j)
  238. }
  239.  
  240. Declaring a Function
  241.  
  242. Here is an example of how do declare a simple Function with a single parameter. A function returns a numerical result, and the type of the result (float, int or boolean) is specified  before the function's name. Load this example function as above. 
  243.  
  244. float Factorial (n)
  245. {
  246.     float f
  247.     int j
  248.  
  249.     f = 1
  250.     For ( j = 1 ; j <= n ; j++ ) f = f * j
  251.     return f
  252. }
  253.  
  254. Here are two examples of how to use the "Factorial" function.
  255.  
  256. Factorial(10)
  257.  
  258. printf ("The natural log of factorial 5 = ", ln( Factorial(5)) )
  259.  
  260.  
  261. One practical use for functions is for unit conversion. Here is an example function which converts inches to centimeters.
  262.  
  263. float inchToCm (inch)
  264. {
  265.     return inch * 2.54
  266. }
  267. inchTocm(5)
  268.  
  269.  
  270. Note: A general unit conversion function is provided in the document
  271.         "Unit Conversions" in the "AxoCalculator AutoLoad" folder.
  272.  
  273.  
  274.  
  275.  
  276. Adding a Program to the Calculator Menu
  277.  
  278. Here is an example of how to declare a simple program. Note that a program has no parameter list, and its name is followed by an optional character string. If present, this string is appended to the calculator menu when the program is loaded. If the second last character of the string is a slash ( / ) then the last character becomes a command key equivalent for running the program. Load the program as above. To run it, type "CountDown" then "enter", or select "Count Down to Lift Off" from the "Calculator" menu. 
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285. program CountDown "Count Down to Lift Off/9"
  286. {
  287.     printf
  288.     printf ("Prepare for count down.")
  289.     FlushOutput
  290. /* Pause */
  291.     for (k = 1 ; k <= 50 ; k++) a = exp(2.0)
  292. /* Start the count down */
  293.     j = 10
  294.     While (j >= 0)
  295.     {
  296.         if (j == 3)  
  297.             printf ("Ignition")
  298.         if (j != 0)  
  299.             printf (j)
  300.         else
  301.             printf ("Lift Off !!")
  302.         FlushOutput
  303.         Beep
  304.         j = j - 1
  305.     /* Slow down the count */
  306.         for (k = 1 ; k <= 20 ; k++) a = exp(2.0)
  307.     }
  308. }
  309.  
  310.  
  311.  
  312.  
  313. Auto-loading a Program
  314.  
  315. A useful program can be automatically loaded every time AxoCalculator is started up. To auto-load one or more programs, simply place them in the folder "AxoCalculator AutoLoad". This folder must be located in the same folder as the AxoCalculator program. 
  316.